package com.qf.commerce.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.commerce.common.dto.ChannelDto;
import com.qf.commerce.common.vo.ChannelVo;
import com.qf.commerce.entity.Channel;
import com.qf.commerce.mapper.ChannelMapper;
import com.qf.commerce.service.ChannelService;
import com.qf.commerce.utils.CommonBeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;
/**
 * @author Administrator
 */
@Service
public class ChannelServiceImpl implements ChannelService {
    @Resource
    ChannelMapper channelMapper;
    @Override
    public int addChannel(ChannelDto channelDto) {
        Channel channel = CommonBeanUtils.copyForBean(channelDto, Channel::new);
        Channel c = channelMapper.selectChannelByName(channel.getChannelName());
        if(!ObjectUtils.isEmpty(c)){
            throw new RuntimeException();
        }
        return channelMapper.insertChannel(channel);
    }

    @Override
    public int modifyChannelById(ChannelDto channelDto) {
        Channel channel = CommonBeanUtils.copyForBean(channelDto, Channel::new);
        return channelMapper.updateChannel(channel);
    }

    @Override
    public int deleteChannelById(Long channelId) {
        return channelMapper.deleteChannelById(channelId);
    }

    @Override
    public int deleteChannelByIds(List<Long> channelIds) {
        return channelMapper.deleteChannelByIds(channelIds);
    }

    @Override
    public PageInfo<ChannelVo> queryChannelList(Integer currentPage) {
        return CommonBeanUtils.copyPage(PageHelper.startPage(currentPage,10).
                doSelectPageInfo(()->channelMapper.selectChannelList()),new PageInfo<ChannelVo>(),ChannelVo::new);
    }

    @Override
    public PageInfo<ChannelVo> queryChannelByBeans(ChannelDto channelDto,Integer currentPage) {
        Channel channel = CommonBeanUtils.copyForBean(channelDto, Channel::new);
        return CommonBeanUtils.copyPage(PageHelper.startPage(currentPage,10).
                doSelectPageInfo(()->channelMapper.selectChannelListByBeans(channel)),new PageInfo<ChannelVo>(),ChannelVo::new);
    }
}
